package cn.jishiyu11.xeqb.activity.personalCenterActivitys

import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.View
import android.widget.TextView
import cn.jishiyu11.xeqb.EntityClass.FragmengData.DiscoverBean
import cn.jishiyu11.xeqb.R
import cn.jishiyu11.xeqb.adapter.DiscoverRecyclerAdapter
import cn.jishiyu11.xeqb.base.BaseActivity
import cn.jishiyu11.xeqb.http.http.i.DataCallBack
import cn.jishiyu11.xeqb.http.http.i.httpbase.DISCOVER_URL
import cn.jishiyu11.xeqb.http.http.i.httpbase.OkHttpManager
import cn.jishiyu11.xeqb.utils.showShortToast
import com.google.gson.Gson
import com.umeng.analytics.MobclickAgent
import kotlinx.android.synthetic.main.act_onlyrecyclerview_layout.*
import kotlinx.android.synthetic.main.footermore.*
import kotlinx.android.synthetic.main.tab_activity_fra.*
import okhttp3.Request
import java.io.IOException

/**
 * Created by jsy_zj on 2018-03-12.
 * 个人中心 ----  抵扣券
 */
class VoucherActicity : BaseActivity(), DataCallBack {

    var DiscList: MutableList<DiscoverBean.Discover>? = null
    var mAdapter: DiscoverRecyclerAdapter? = null
    var discBean: DiscoverBean? = null
    var mIsRefresh = false
    var mIsAddMore = false
    var page = 1


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.act_onlyrecyclerview_layout)
        findViewById()
    }

    override fun findViewById() {
        title_image.visibility = View.VISIBLE
        title_image.setOnClickListener { finish() }
        (title_view as TextView).text = this.getString(R.string.personalcenter_vouchers)


        act_onlyrecyclerview_layout_recyclerv.layoutManager = LinearLayoutManager(this)
        DiscList = ArrayList<DiscoverBean.Discover>()
        mAdapter = DiscoverRecyclerAdapter(this!!, DiscList!!)
        act_onlyrecyclerview_layout_recyclerv.adapter = mAdapter

        act_onlyrecyclerview_layout_recyclerv.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrollStateChanged(recyclerView: RecyclerView?, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)
                var layoutManager: LinearLayoutManager = recyclerView?.layoutManager as LinearLayoutManager
                var lastPosition = layoutManager.findLastVisibleItemPosition()
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastPosition == DiscList!!.size) {
                    if (!mIsAddMore) {
                        mIsAddMore = true
                        if (DiscList?.size != 0) {
                            page += 1
                            getData()
                        }
                    }
                }
            }
        })
        act_onlyrecyclerview_layout_refreshlayout.setOnRefreshListener {
            if (!mIsRefresh) {
                mIsRefresh = true
                page = 1
                getData()
            }
        }
        getData()

    }

    override fun initView() {

    }

    /**
     * recycler 数据刷新
     * */
    private fun setRecyclerData(result: String) {

        if (!result.isNullOrEmpty()) {

            if (mIsRefresh) {
                mIsRefresh = false
                DiscList!!.clear()
                act_onlyrecyclerview_layout_refreshlayout.isRefreshing = false
            }
            discBean = Gson().fromJson(result, DiscoverBean::class.java)

//            when (discBean?.code!!.toInt()) {
//                200 -> {
//                    var msgList = discBean?.data
//                    if (msgList != null) {
//                        DiscList!!.addAll(msgList!!)
//                    }
//                    if (mIsAddMore) mIsAddMore = false
//                    mAdapter?.notifyDataSetChanged()
//                    footer_layout_text.setText(getString(R.string.load_more_pleace_wait))
//                    footer_layout_loading.visibility = View.VISIBLE
//                }
//                205 -> {
//                    if (mIsAddMore) {
//                        mIsAddMore = false
//                        if (page > 1) page -= 1
//                        footer_layout_text.setText(getString(R.string.load_more_no_more))
//                        footer_layout_loading.visibility = View.GONE
//                    }
//                }
//                300 -> {
//                    if (mIsAddMore) {
//                        mIsAddMore = false
//                        if (page > 1) page -= 1
//                    }
//                    showShortToast("系统关闭中！")
//                }
//            }
        }
    }


    /**
     * 获取数据
     * */
    private fun getData() {
        OkHttpManager.postAsync(DISCOVER_URL, "VOUCHER_URL", null, this)
    }

    override fun requestFailure(request: Request, name: String, e: IOException) {
        this.showShortToast("数据获取失败！")
    }

    override fun requestSuccess(result: String, name: String) {
        when (name) {
            "VOUCHER_URL" -> {
                result.trim()
                setRecyclerData(result)
            }
            else -> {
            }
        }

    }


    override fun onResume() {
        super.onResume()
        MobclickAgent.onResume(this)
    }

    override fun onPause() {
        super.onPause()
        MobclickAgent.onPause(this)
    }


}
